package com.maple.exclusivesport;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MyThread extends Thread {

    public interface OnTextTipsListener{
        void onTips(String text,String num);
    }

    public OnTextTipsListener listener;

    String title="热身运动";


    public MyThread(Context context,OnTextTipsListener onTextTipsListener) {
        this.listener=onTextTipsListener;
        this.context = context;
        titleList = new ArrayList<>();
        titleList.add("提膝跳跃");
        titleList.add("胯下击掌");
        titleList.add("深蹲跳");
        titleList.add("高抬腿");
        titleList.add("开合跳");
        titleList.add("原地冲刺跑");
    }

    private final Object lock = new Object();

    private boolean pause = false;

    public boolean isPause() {
        return pause;
    }

    public void setPause(boolean pause) {
        this.pause = pause;
    }

    /**
     * 调用该方法实现线程的暂停
     */
    public void pauseThread() {
        pause = true;
    }


    /*
    调用该方法实现恢复线程的运行
     */
    public void resumeThread() {
        pause = false;
        synchronized (lock) {
            lock.notify();
        }
    }

    /**
     * 这个方法只能在run 方法中实现，不然会阻塞主线程，导致页面无响应
     */
    void onPause() {
        synchronized (lock) {
            try {
                lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run() {
        super.run();
        while (true) {
            while (pause) {
                onPause();
            }
            try {
                TTSUtils.getSingleton(context).tts.speak("请开始30秒热身运动", TextToSpeech.QUEUE_ADD, null);
                Thread.sleep(3000);
                threeTTS();
                for (int j = 0; j < 4; j++) {
                    for (int k = 0; k < 6; k++) {
                        title=titleList.get(k);

                        TTSUtils.getSingleton(context).tts.speak("请开始30秒" + titleList.get(k) + "运动", TextToSpeech.QUEUE_ADD, null);

                        threeTTS();
                    }
                    TTSUtils.getSingleton(context).tts.speak("请休息30秒", TextToSpeech.QUEUE_ADD, null);
                    threeTTS();
                }
                title="跳绳运动";
                TTSUtils.getSingleton(context).tts.speak("请开始20分钟跳绳运动", TextToSpeech.QUEUE_ADD, null);

                 for(int i=0;i<20 * 60;i++){
                     while (pause) {
                         onPause();
                     }
                     Thread.sleep( 1000);
                     listener.onTips(title+" 倒计时(秒)",""+(20 * 60-i));
                 }

                TTSUtils.getSingleton(context).tts.speak("恭喜你，打卡成功！", TextToSpeech.QUEUE_ADD, null);
                title="恭喜你，打卡成功！";
                listener.onTips(title,"");

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    Context context;
    List<String> titleList;

    public void threeTTS() throws InterruptedException {
        Thread.sleep(3000);
        for (int i = 1; i < 31; i++) {
            Thread.sleep(1000);
            TTSUtils.getSingleton(context).tts.speak(i + "", TextToSpeech.QUEUE_ADD, null);
            listener.onTips(title+" 倒计时(秒)",""+(31-i));
            while (pause) {
                onPause();
            }

        }
    }


}

